package cn.mogujun.calendargui.gui.dataNum;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class DataDay extends JPanel {

	ArrayList<ArrayList<JLabel>> days;

	public DataDay() {
		setLayout(new GridBagLayout());

		initTable();
		setVisible(true);
	}

	private void initTable() {
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridheight = 1;
		constraints.gridwidth = 1;
		constraints.weightx = 1;
		constraints.weighty = 1;
		constraints.anchor = GridBagConstraints.CENTER;
		constraints.fill = GridBagConstraints.BOTH;
		constraints.ipadx = 0;
		constraints.ipady = 0;
		// constraints.insets;

		days = new ArrayList<ArrayList<JLabel>>();

		for (int i = 0; i < 7; i++) {
			ArrayList<JLabel> dayLine = new ArrayList<JLabel>();
			for (int j = 0; j < 7; j++) {

				JLabel label = new JLabel();
				label.setHorizontalAlignment(SwingConstants.CENTER);
				dayLine.add(label);
				constraints.gridx = j;
				constraints.gridy = i;
				if (i == 0) {
					switch (j) {
					case 0:
						label.setText("日");
						break;
					case 1:
						label.setText("一");
						break;
					case 2:
						label.setText("二");
						break;
					case 3:
						label.setText("三");
						break;
					case 4:
						label.setText("四");
						break;
					case 5:
						label.setText("五");
						break;
					case 6:
						label.setText("六");
						break;
					}
					label.setBackground(Color.WHITE);
					label.setForeground(Color.BLUE);
					label.setFont(new Font("Dialog", 1, 28));
				} else {
					label.setText("  ");

					label.setBackground(Color.WHITE);
					label.setForeground(Color.black);
					label.setFont(new Font("Dialog", 1, 26));
				}

				label.setOpaque(true);
				add(label, constraints);
			}
			days.add(dayLine);
		}
		Calendar calendar = Calendar.getInstance();
		setYearAndMonth(calendar.get(calendar.YEAR),
				calendar.get(calendar.MONTH) + 1);
	}

	public ArrayList<ArrayList<JLabel>> getDays() {
		return days;
	}

	public void setDays(ArrayList<ArrayList<JLabel>> days) {
		this.days = days;
	}

	public void setYearAndMonth(int Year, int Month) {
		// 将面板初始化
		for (int i = 1; i <= 6; i++) {
			for (int j = 0; j < 7; j++) {
				days.get(i).get(j).setText(" ");
			}
		}
		int Flag, date, ye, ar, mo, week;// Flag判断平年与闰年，date是月的天数，
		// ye是year的前两位，ar是year的后两位，mo表示月数，week表示星期；
		ye = Year / 100;
		ar = Year % 100;
		if (Month == 1) {
			mo = 13;

			if (ar == 0) {
				ar = 99;
				ye--;
			} else {
				ar--;
			}
		}// 一月mo当13；
		else if (Month == 2) {
			mo = 14;
			if (ar == 0) {
				ar = 99;
				ye--;
			} else {
				ar--;
			}
		}// 二月mo当14
		else
			mo = Month;
		if (Year % 4 == 0)// 判断年份是平年还是闰年；
			if (Year % 100 == 0)
				if (Year % 400 == 0)
					Flag = 1;
				else
					Flag = 0;
			else
				Flag = 1;
		else
			Flag = 0;
		// 月的天数；
		if (Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8
				|| Month == 10 || Month == 12)
			date = 31;
		else if (Month == 4 || Month == 6 || Month == 9 || Month == 11)
			date = 30;
		else if (Flag == 0)
			date = 28;
		else
			date = 29;
		// 星期；
		week = (ar + ar / 4 + ye / 4 - 2 * ye + 26 * (mo + 1) / 10);
		while (week < 0) {
			week += 7;
		}
		week = week % 7;

		for (int i = 0; i < date; i++) {
			days.get((week + 7 + i) / 7).get((week + i) % 7)
					.setText(String.valueOf(i + 1));
		}
	}

}
